package com.ivncorp.evnproject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.astuetz.PagerSlidingTabStrip.IconTabProvider;

public class ActivityMain extends BaseActivity {

	private PagerSlidingTabStrip tabs;
	private ViewPager pager;
	private MyPagerAdapter adapter;
	private static long back_pressed;

	public ActivityMain() {
		super(R.string.app_name);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		pager = (ViewPager) findViewById(R.id.pager);
		adapter = new MyPagerAdapter(getSupportFragmentManager());
		pager.setAdapter(adapter);

		final int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
						.getDisplayMetrics());
		pager.setPageMargin(pageMargin);
		tabs.setViewPager(pager);
	}

	@Override
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis())
			super.onBackPressed();
		else
			Toast.makeText(getBaseContext(), "Press once again to exit!",
					Toast.LENGTH_SHORT).show();
		back_pressed = System.currentTimeMillis();

	}

	public void switchContent(int position) {
		Intent intent = null;
		switch (position) {
		case 0:
			getSlidingMenu().toggle();
			break;
		case 1:
			intent = new Intent();
			intent.setClass(ActivityMain.this, ActivityProfile.class);
			break;
		case 2:
			intent = new Intent();
			intent.setClass(ActivityMain.this, AboutActivity.class);
			break;
		case 3:
			getSlidingMenu().toggle();
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.exit)
					.setTitle(R.string.exit_title)
					.setPositiveButton(R.string.co,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									SharedPreferences pre = getSharedPreferences(
											"Login_Setting", MODE_PRIVATE);
									SharedPreferences.Editor edit = pre.edit();
									edit.putBoolean("Login_Success", false);
									edit.commit();
									startActivity(new Intent(ActivityMain.this,ActivityLogin.class));
									ActivityMain.this.finish();
									
								}
								
							})
					.setNegativeButton(R.string.khong,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									// Handle Cancel

								}
							}).show();

			break;
		}

		if (intent != null) {
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.left_out);
		}
	}

	
	public class MyPagerAdapter extends FragmentPagerAdapter implements
			IconTabProvider {

		private final int[] ICONS = { R.drawable.ic_action_map_dark,
				R.drawable.ic_action_time, R.drawable.ic_action_picture_dark };

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		/*
		 * @Override public CharSequence getPageTitle(int position) { return
		 * TITLES[position]; }
		 */
		@Override
		public int getCount() {
			return ICONS.length;
		}

		@Override
		public int getPageIconResId(int position) {
			// TODO Auto-generated method stub
			return ICONS[position];
		}

		@Override
		public Fragment getItem(int position) {

			switch (position) {
			case 0:
				return new FragmentMap();
			case 1:
				return new FragmentTimeLine();
			case 2:
				return new FragmentDashboard();
			}
			return null;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			// super.destroyItem(container, position, object);
		}

	}

}